//
//  NoteNetworkManager.m
//  todoApp
//
//  Created by wangyaqing on 15/4/18.
//  Copyright (c) 2015年 ll studio. All rights reserved.
//

#import "NoteNetworkManager.h"
#import "AFNetworking.h"
#import "AFURLRequestSerialization.h"

@implementation NoteNetworkManager
{
    AFHTTPRequestOperation *requestOperation;
}


-(void)getContentAtURL:(NSString *)url param:(NSDictionary *)param errorHandler:(void (^)(NSError *err))errorBlock successHandler:(void (^)(id))successBlock
{
    NSLog(@"GET请求的url为 ： %@ \n", url);
    
    [self cancelAndDiscardURLConnection];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    self.isLoading = YES;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer =  [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
    
    __weak NoteNetworkManager *wself = self;
    requestOperation = [manager GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        wself.isLoading = NO;
        successBlock(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        NSLog(@"GET 请求出错了 \n ----------------------------Error: %@--------------------------\n URL:%@", error, url);

        wself.isLoading = NO;
        errorBlock(error);
    }];
    
}

/*- (void)postJsonAtURL:(NSString *)url param:(NSDictionary *)param errorHandler:(void (^)(NSError *))errorBlock successHandler:(void (^)(id))successBlock
{
    
}
-(void)postContentAtURL:(NSString *)url param:(NSDictionary *)param errorHandler:(void (^)(NSError *err))errorBlock successHandler:(void (^)(id))successBlock
{
   
}
 */

- (void)uploadContentAtURL:(NSString *)url param:(NSDictionary *)param andData:(NSData *)data errorHandler:(void (^)(NSError *))errorBlock successHandler:(void (^)(id))successBlock
{
    NSLog(@"POST FORM 请求的url为 ： %@ \n", url);
    
    [self cancelAndDiscardURLConnection];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    self.isLoading = YES;
    __weak typeof(self) wSelf = self;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    requestOperation = [manager POST:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:data name:@"image" fileName:@"avatar.png" mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        wSelf.isLoading = NO;
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        successBlock(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        wSelf.isLoading = NO;
        errorBlock(error);
    }];
}

-(void)cancelAndDiscardURLConnection
{
    NSLog(@"\n-----------------http 请求取消------------\n");
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [requestOperation cancel];
}


-(void)dealloc
{
    [self cancelAndDiscardURLConnection];
}

@end
